<?php
require_once(ABSPATH. 'include/control/common.php.inc');
require_once(ABSPATH. 'include/model/Category.php');
require_once(ABSPATH. 'include/model/Post.php');
require_once(ABSPATH. 'include/dao/PostDAO.php');
require_once(ABSPATH. 'include/dao/UserDAO.php');

function getJobsByUser(){
	$offset = isset($_GET["offset"]) ? $_GET["offset"] : 0 ; 
	
	$userId = $_GET["id"];
	$udao = new UserDAO();
	$model-> User = $udao->get($userId);	
	
	$dao = new PostDAO();
	$jobs = $dao-> get_userId($userId, $offset);
	if(!$jobs){
	}

	$model-> jobs = $jobs;
	$model-> realCount = count($jobs);
	$model-> offset = $offset;
	$model-> recordCount = $dao->getUserRecordsCount(false);
	$model-> pageCount = getPageCount($model-> recordCount);
	
	
	
	return $model;
}

function getjobs(){
	$offset = isset($_GET["offset"]) ? $_GET["offset"] : 0 ; 	
	
	$category = getCategory($_GET["category"]);	
	if(!$category){
		throw  new Exception("category not exist", "1");
	}
	
	$dao = new PostDAO();
	$jobs = $dao-> get_categoryId($category->id, $offset);
	if(!$jobs){
	}	
	
	$model-> category = $category;
	$model-> realCount = count($jobs);
	$model-> jobs = $jobs;
	$model-> offset = $offset;
	$model-> recordCount = $dao->getRecordsCount($category->id);
	$model-> pageCount = getPageCount($model-> recordCount); 
	
	return $model;	
}

function getAllJobs(){
	$offset = isset($_GET["offset"]) ? $_GET["offset"] : 0 ; 
	$dao = new PostDAO();
	$jobs = $dao-> get_all($offset);
	
	$model-> jobs = $jobs;
	$model-> realCount = count($jobs);
	$model-> offset = $offset;
	$model-> recordCount = $dao->getRecordsCount(false);
	$model-> pageCount = getPageCount($model-> recordCount);
	
	return $model;	
}

function getPageCount($recordsCount){
	$recordsCount%20 ==0?$recordsCount/20 : $recordsCount/20+1;
}
?>